package com.controller;

import com.domain.UserInfo;
import com.feign.SmsFeignApi;
import com.feign.UserInfoFeignApi;
import com.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private UserInfoFeignApi userInfoFeignApi;
    @Autowired
    private SmsFeignApi smsFeignApi;

    @GetMapping("/get")
    public Object get(String id){
        JsonResult result = userInfoFeignApi.get(id);
        //约定表示降级方法执行的结果是result为null，即如果result为null，说明是降级方法
        if (result == null){
            return JsonResult.defaultError();
        }
        return result;
    }

    //true表示不存在  false表示手机号码存在
    @GetMapping("/checkPhone")
    public JsonResult checkPhone(String phone){
        JsonResult result = userInfoFeignApi.checkPhone(phone);
        //约定表示降级方法执行的结果是result为null，即如果result为null，说明是降级方法
        if (result == null){
            return JsonResult.defaultError();
        }
        return result;
    }

    @PostMapping("/regist")
    public JsonResult regist(String phone,String nickname,String password,String rpassword,String verifyCode){
        //先调用msg-spi进行短信验证
        JsonResult verifyCodeResult = smsFeignApi.checkVerifyCode(phone, verifyCode);
        //再调用member-api实现用户注册
        JsonResult result = userInfoFeignApi.regist(phone,nickname,password,rpassword);
        if (verifyCodeResult == null || result == null){
            return JsonResult.defaultError();
        }
        return result;
    }
}